package cn.cdeden.knowledge.dubbo;

import cn.cdeden.common.oss.enumd.OssBucketOneEnum;
import cn.cdeden.common.oss.enumd.OssModuleOperate;
import cn.cdeden.common.oss.enumd.OssModuleType;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.Team;
import cn.cdeden.knowledge.domain.TeamFilesTree;
import cn.cdeden.knowledge.domain.TeamTemplateTree;
import cn.cdeden.knowledge.domain.dto.FilesTree;
import cn.cdeden.knowledge.domain.dto.SysMessageDto;
import cn.cdeden.knowledge.domain.enums.MessageTypeEnums;
import cn.cdeden.knowledge.enums.MessageFileTypeEnums;
import cn.cdeden.knowledge.enums.TeamParamEnum;
import cn.cdeden.knowledge.enums.TreeTypeEnum;
import cn.cdeden.knowledge.service.*;
import cn.cdeden.resource.api.OssUploadService;
import cn.cdeden.resource.api.domain.*;
import cn.cdeden.system.api.RemoteUserService;
import cn.cdeden.system.api.model.LoginUser;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.lock.annotation.Lock4j;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Stack;
import java.util.stream.Collectors;

/**
 * @ProjectName : KnowCore01
 * @Package : cn.cdeden.knowledge.dubbo
 * @ClassName : OssUploadEnterpriseServiceAImpl
 * @Author : Yu.
 * @Description : // 企业的文件上传服务
 * @Date : 2024/12/6 16:08
 * @Version : 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
@DubboService(group = "teamTemplate", version = "1.0.0")
public class OssUploadTeamTemplateServiceImpl implements OssUploadService {

    private final TeamTemplateTreeService teamTemplateTreeService;

    private final TeamRelevanceRoleService teamRelevanceRoleService;

    @Override
    public RemoteFile getFileDataInfo(String moduleId, Long userId, JSONObject info, OssModuleOperate operate) {

        TeamTemplateTree teamTemplateTree = teamTemplateTreeService.getById(moduleId);
        if (teamTemplateTree==null){
            throw new RuntimeException("不存在此文件");
        }

        if (operate == OssModuleOperate.upload) {
            if (!teamRelevanceRoleService.checkPermission(moduleId, String.valueOf(teamTemplateTree.getTeamId()), LoginHelper.getLoginUserByUserId(userId), TeamParamEnum.TEMPLATE.getParam())) {
                throw new RuntimeException("无控制模板权限！");
            }
        }

        RemoteFile remoteFile = new RemoteFile();
        remoteFile.setOssId(teamTemplateTree.getStorageId() == null ? null : Long.valueOf(teamTemplateTree.getStorageId()));
//        remoteFile.setVersionId(teamTemplateTree.getEdition());
        remoteFile.setVersionName(teamTemplateTree.getEditionName());
        remoteFile.setFileSize(teamTemplateTree.getSize());
        remoteFile.setFileSuffix(teamTemplateTree.getSuffix());
        remoteFile.setName(teamTemplateTree.getRealName());
        remoteFile.setOriginalName(teamTemplateTree.getRealName());
        remoteFile.setModuleType(OssModuleType.TeamTemplate.getCode());
        remoteFile.setRead(true);
        remoteFile.setEdit(true);
        remoteFile.setDownload(true);
        remoteFile.setPrint(true);


        return remoteFile;
    }

    @Override
    public RemoteOssUpload verifyData(RemoteOssUpload remoteOssUpload, Long userId) {
        remoteOssUpload.setBrandName(OssBucketOneEnum.TEAM_BUCKET.getCode());
        return remoteOssUpload;
    }

    @Override
    public RemoteFilesBucketPath upload(RemoteFilesUpload upload, Long userId) {
        return RemoteFilesBucketPath.of(OssBucketOneEnum.TEAM_BUCKET.getCode());
    }

    @Override
    public List<RemoteFilesDownload> downloadFile(List<String> idList, Long userId, JSONObject jsonObject) {
        return null;
    }

}

